import requests
from src.config import settings


# 调用togetherai的API, 生成回答

def get_text(role, content):
    return [{"role": role, "content": content}]


def check_length(text):
    while sum(len(content["content"]) for content in text) > 8000:
        text.pop(0)
    return text


api_token = settings.TOGETHER_AI_TOKEN

model = "google/gemma-7b-it"

question = check_length(get_text("user", "你好"))

res = requests.post('https://api.together.xyz/v1/chat/completions', json={
    "model": model,
    "max_tokens": 512,
    "temperature": 0.7,
    "top_p": 0.7,
    "top_k": 50,
    "repetition_penalty": 1,
    "stop": ["<eos>", "<end_of_turn>"],
    "messages": question
}, headers={
    "Authorization": "Bearer " + api_token,
})

print(res.text)
